﻿@page "/post/{year:int:min(1900):length(4)}/{month:int:range(1,12)}/{day:int:range(1,31)}/{slug:regex(^(?!-)([a-zA-Z0-9-]){{1,128}}$)}"
@model Moonglade.Web.Pages.PostModel
@using Moonglade.Utils

@Html.AntiForgeryToken()

@{
    ViewBag.BodyClass = "body-post-slug bg-gray-1";
    if (!Model.Post.IsFeedIncluded)
    {
        HttpContext.Response.Headers.Add("X-Robots-Tag", "noindex, nofollow");
    }

    ViewBag.PostLangCode = Model.Post.ContentLanguageCode;
    ViewBag.MetaDescription = Model.Post.ContentAbstract;

    @if (Model.Post.Tags.Any())
    {
        ViewBag.Keywords = string.Join(", ", Model.Post.Tags.Select(t => t.NormalizedName));
    }
}

@section meta {
    @if (!Model.Post.IsFeedIncluded)
    {
        <meta name="robots" content="noindex, nofollow" />
    }

    <meta name="title" content="@Model.Post.Title" />
    <meta name="displaydate" content="@Model.Post.PubDateUtc.GetValueOrDefault().ToString("u")">
    <meta name="copyright" content="(C) @DateTime.UtcNow.Year @BlogConfig.GeneralSettings.SiteTitle">
    <meta name="author" content="@(string.IsNullOrWhiteSpace(Model.Post.Author) ? BlogConfig.GeneralSettings.OwnerName : Model.Post.Author) " />
    @if (BlogConfig.GeneralSettings.UseDublinCoreMetaData)
    {
        <partial name="_DublinCoreMetaData" model="Model" />
    }

    @if (BlogConfig.AdvancedSettings.EnableWebmention)
    {
        <link rel="webmention" href="@(Helper.ResolveCanonicalUrl(BlogConfig.GeneralSettings.CanonicalPrefix, "webmention"))">
    }
}

@section head {
    <link href="~/css/post.css" rel="stylesheet" asp-append-version="true" />
    <link href="~/css/highlighter-custom.css" rel="stylesheet" asp-append-version="true" />
    @if (BlogConfig.ContentSettings.DocumentOutline)
    {
        <link href="~/css/outline.css" rel="stylesheet" />
    }
}

@section opengraph {
    <meta property="og:site_name" content="@BlogConfig.GeneralSettings.SiteTitle">
    <meta property="og:title" content="@Model.Post.Title">
    <meta property="og:type" content="article" />
    <meta property="og:description" content="@Model.Post.ContentAbstract" />
    @if (!string.IsNullOrWhiteSpace(BlogConfig.GeneralSettings.CanonicalPrefix))
    {
        <meta property="og:url" content="@(Helper.ResolveCanonicalUrl(BlogConfig.GeneralSettings.CanonicalPrefix, HttpContext.Request.Path))" />
    }
}

@section scripts {
    <script src="~/js/3rd/highlight.min.js" asp-append-version="true"></script>
    <script src="~/js/3rd/katex.min.js"></script>

    @if (BlogConfig.ContentSettings.DocumentOutline)
    {
        <script src="~/js/app/documentoutline.js"></script>
        <script>
            let outline = new DocumentOutline(
                '.post-content h1, ' +
                '.post-content h2, ' +
                '.post-content h3, ' +
                '.post-content h4, ' +
                '.post-content h5');
        </script>
    }
    <script type="module">
        import * as viewpost from '/js/app/viewpost.module.js'
        import * as utils from '/js/app/utils.module.js'

        window.viewpost = viewpost;

        viewpost.resizeImages();
        if (window.innerWidth >= 768) {
            viewpost.applyImageZooming();
        }

        viewpost.renderCodeHighlighter();
        viewpost.RenderLaTeX();
        viewpost.calculateReadingTime();

        @if (BlogConfig.AdvancedSettings.WarnExternalLink)
        {
            <text>
                viewpost.warnExtLink();
            </text>
        }

        window.fitImageToDevicePixelRatio = @BlogConfig.ImageSettings.FitImageToDevicePixelRatio.ToString().ToLower();

        document.getElementById('comment-form').addEventListener('submit', function (e) {
            e.preventDefault();
            let pid = document.querySelector('article').dataset.postid;
            viewpost.submitComment(pid);
        });

        utils.formatUtcTime(false);
    </script>
}

<article class="article-post-slug position-relative rounded-3 shadow-sm border bg-white mb-4" data-postid="@Model.Post.Id" lang="@Model.Post.ContentLanguageCode">
    @if (Model.Post.IsFeatured)
    {
        <partial name="_FeaturedTag" />
    }

    <header class="post-header mb-4 @(BlogConfig.ContentSettings.PostTitleAlignment == PostTitleAlignment.Center ? "text-center" : null)">
        <h1 class="post-title">
            @Model.Post.Title
        </h1>

        <div class="post-publish-info text-secondary mb-2">
            @if (!string.IsNullOrWhiteSpace(Model.Post.Author))
            {
                <i class="bi-person d-none d-sm-inline"></i>
                <span class="me-2 d-none d-sm-inline">@Model.Post.Author</span>
            }

            <i class="bi-calendar-event"></i>
            <span class="me-2">
                <time data-utc-label="@Model.Post.PubDateUtc.GetValueOrDefault().ToString("u")">
                    @DateOnly.FromDateTime(Model.Post.PubDateUtc.GetValueOrDefault()) (UTC)
                </time>
            </span>
            
            <i class="bi-clock"></i>
            <span id="reading-time"></span>
        </div>
    </header>

    @if (Model.Post.IsOutdated)
    {
        <partial name="_PostOutdatedMessage" />
    }

    @if (!string.IsNullOrWhiteSpace(Model.Post.HeroImageUrl))
    {
        <div class="post-hero-image-container text-center mb-3">
            <img src="@Helper.SterilizeLink(Model.Post.HeroImageUrl)" class="post-hero-image img-fluid rounded-3 shadow-sm" />
        </div>
    }

    <section class="post-content clearfix">
        <partial name="_PostContentRender" model="Model.Post.PostContent" />
    </section>

    @if (BlogConfig.ContentSettings.ShowPostFooter)
    {
        <section class="post-footer clearfix">
            @Html.Raw(BlogConfig.ContentSettings.PostFooterHtmlPitch)
        </section>
    }

    <partial name="_PostActions" model="Model.Post" />
</article>

@if (BlogConfig.ContentSettings.EnableComments)
{
    var days = DateTime.UtcNow.Date.Subtract(Model.Post.PubDateUtc.GetValueOrDefault()).Days;

    if (BlogConfig.ContentSettings.CloseCommentAfterDays > 0 && days > BlogConfig.ContentSettings.CloseCommentAfterDays)
    {
        <div class="text-muted text-center">
            <span class="bi-dash-circle"></span>
            @SharedLocalizer["Comment is closed."]
        </div>
    }
    else
    {
        switch (BlogConfig.ContentSettings.CommentProvider)
        {
            case CommentProvider.BuiltIn:
                <div class="d-print-none">
                    @if (Model.Post.CommentEnabled)
                    {
                        <section class="comment-form-containter rounded-3 shadow-sm border bg-white mb-4">
                            <h5 class="card-subtitle mb-3 text-muted">
                                <i class="bi-chat-left-text"></i>
                                @SharedLocalizer["Comments"]
                            </h5>
                            <partial name="_CommentForm" />
                        </section>

                        <div id="thx-for-comment" class="alert alert-warning" style="display: none;">
                            @SharedLocalizer["Thanks, your comment is pending approval now."] <br />
                            @SharedLocalizer["It will show up once blog administrator approved your comment."]
                        </div>

                        <div id="thx-for-comment-non-review" class="alert alert-success" style="display: none;">
                            @SharedLocalizer["Thanks for your comment."] <br />
                            @SharedLocalizer["Refresh the page to see your comment."]
                        </div>

                        <section id="comments-list">
                            @await Component.InvokeAsync("CommentList", new { postId = Model.Post.Id })
                        </section>
                    }
                    else
                    {
                        <div class="text-muted text-center">
                            <span class="bi-ban"></span>
                            @SharedLocalizer["Comment of this post is disabled."]
                        </div>
                    }
                </div>
                break;
            case CommentProvider.ThirdParty:
                @Html.Raw(BlogConfig.ContentSettings.ThirdPartyCommentHtmlPitch)
                break;
        }
    }
}

<div class="modal fade" id="imgzoomModal" tabindex="-1" role="dialog" aria-labelledby="imgzoomModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-imgzoom" role="document" style="max-width: 100%">
        <div class="modal-content">
            <img id="imgzoom" alt="imgzoom" class="img-fluid" />
        </div>
    </div>
</div>

@if (BlogConfig.AdvancedSettings.WarnExternalLink)
{
    <partial name="_ExternalLink" />
}

<partial name="_PostForestageAdmin" />
